package lzyc.pointsManagement.web.controller.business;

import lzyc.pointsManagement.business.attachment.dto.AttachmentsDto;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.common.json.JSON;
import lzyc.pointsManagement.common.response.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RequestMapping("/api/v1/attachments")
@RestController
public class AttachmentsController {

    @Autowired
    AttachmentsService attachmentsService;

    @PostMapping("/upload")
    public ResponseMessage fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("attachmentInfo") String attachmentInfo) {
        AttachmentsDto dto;
        try {
            dto = JSON.unmarshal(attachmentInfo, AttachmentsDto.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return attachmentsService.fileUpload(file, dto);
    }

    @GetMapping("/getattachmentbyid")
    public ResponseMessage getAttachmentById(@RequestParam("id") String id) {
        return attachmentsService.selectAttachmentsDtoById(id);
    }
}
